// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <cantor/parser/ast/ddl.h>
#include <cantor/parser/ast/stmt.h>

namespace cantor::parser {

    void ColumnDef::to_stream(std::ostream &os) const {
        os << "ColumnDef:{";
        os << "name:";
        if (name) {
            os << name->to_string();
        } else {
            os << "null";
        }

        os << " type_name:";
        if (type_name) {
            type_name->to_stream(os);
        } else {
            os << "null";
        }
        os << " options:[";
        bool first = true;
        for (auto i = 0; i < options.size(); i++) {
            if (!first) {
                os << ", ";
            }
            first = false;
            options[i]->to_stream(os);
        }
        os << "]";
        os << "}";
    }

    nlohmann::ordered_json IndexOption::to_json() const {
        nlohmann::ordered_json j =DefiniteExpr::to_json_impl();
        j["comment"] = comment.to_string();
        if (lock) {
            j["lock"] = lock->to_json();
        } else {
            j["lock"] = nlohmann::ordered_json();
        }
        if (algo) {
            j["algo"] = algo->to_json();
        } else {
            j["algo"] = nlohmann::ordered_json();
        }
        j["index_type"] = index_type;
        return j;
    }

    nlohmann::ordered_json Constraint::to_json() const {
        nlohmann::ordered_json j = DefiniteExpr::to_json_impl();
        j["constraint_type"] = pb::ast::ConstraintType_Name(type);
        j["constraint_name"] = name.to_string();
        auto cols = nlohmann::ordered_json::array();
        for (auto i = 0; i < columns.size(); i++) {
            cols.push_back(columns[i]->to_json());
        }
        j["columns"] = cols;
        if (index_option) {
            j["index_option"] = index_option->to_json();
        } else {
            j["index_option"] = nlohmann::ordered_json();
        }
        j["index_dist"] = static_cast<int>(index_dist);
        if (check_expr) {
            j["check_expr"] = check_expr->to_json();
        }
        return j;
    }

    nlohmann::ordered_json TableOption::to_json() const {
        nlohmann::ordered_json j = DefiniteExpr::to_json_impl();
        auto str = pb::ast::TableOptionType_Name(type);
        j["option_type"] =  str;
        j["str_value"] = str_value.to_string();
        j["uint_value"] = uint_value;
        if (partition) {
            j["partition"] = partition->to_json();
        }
        return j;
    }

    nlohmann::ordered_json ColumnDef::to_json() const {
        nlohmann::ordered_json j = DefiniteExpr::to_json_impl();
        if (name) {
            j["name"] = name->to_json();
        } else {
            j["name"] = nlohmann::ordered_json{};
        }

        if (type_name) {
            j["type_name"] = type_name->to_json();
        } else {
            j["type_name"] = nlohmann::ordered_json{};
        }
        nlohmann::ordered_json ops = nlohmann::ordered_json::array();
        for (auto i = 0; i < options.size(); i++) {
            ops.push_back(options[i]->to_json());
        }
        j["options"] = ops;
        return j;
    }

    nlohmann::ordered_json EnumElement::to_json() const {
        nlohmann::ordered_json j = DefiniteExpr::to_json_impl();
        j["name"] = name.to_string();
        if (value) {
            j["value"] = value->to_json();
        }
        return j;
    }

    nlohmann::ordered_json ColumnOption::to_json() const {
        nlohmann::ordered_json j = DefiniteExpr::to_json_impl();
        j["column_opt_type"] = pb::ast::ColumnOptionType_Name(type);
        j["store"] = store;
        if (expr) {
            j["expr"] = ((DispatchExpr *) expr)->to_json();
        } else {
            j["expr"] = nlohmann::ordered_json{};
        }
        return j;
    }

    nlohmann::ordered_json AlterTableSpec::to_json() const {
        nlohmann::ordered_json j = DefiniteExpr::to_json_impl();

        j["spec_type"] = spec_type;
        j["column_name"] = column_name.to_string();
        auto tb_ops = nlohmann::ordered_json::array();
        for (auto i = 0; i < table_options.size(); ++i) {
            tb_ops.emplace_back(table_options[i]->to_json());
        }

        j["table_options"] = tb_ops;
        if (new_table_name) {
            j["new_table_name"] = new_table_name->to_json();
        } else {
            j["new_table_name"] = nlohmann::ordered_json();
        }

        auto new_c = nlohmann::ordered_json::array();
        for (auto i = 0; i < new_columns.size(); ++i) {
            new_c.emplace_back(new_columns[i]->to_json());
        }
        j["new_columns"] = new_c;

        auto set_list_ops = nlohmann::ordered_json::array();
        for (auto i = 0; i < set_list.size(); ++i) {
            set_list_ops.push_back(set_list[i]->to_json());
        }
        j["set_list"] = set_list_ops;

        if (where) {
            j["where"] = where->to_json();
        } else {
            j["where"] = nlohmann::ordered_json();
        }
        auto cons = nlohmann::ordered_json::array();
        for (auto i = 0; i < new_constraints.size(); ++i) {
            cons.push_back(new_constraints[i]->to_json());
        }

        j["new_constraints"] = cons;

        j["index_name"] = index_name.to_string();
        j["resource_tag"] = resource_tag.to_string();
        j["force"] = force;

        j["is_virtual_index"] = is_virtual_index;
        j["is_unique_indicator"] = is_unique_indicator;
        if (partition_range) {
            j["partition_range"] = partition_range->to_json();
        } else {
            j["partition_range"] = nlohmann::ordered_json();
        }

        return j;
    }

    nlohmann::ordered_json TablePartitionOption::to_json() const {
        nlohmann::ordered_json j = DefiniteExpr::to_json_impl();
        j["partition_type"] = partition_type;
        j["partition_num"] = partition_num;
        if (expr) {
            j["expr"] = ((DispatchExpr *) expr)->to_json();
        } else {
            j["expr"] = nlohmann::ordered_json{};
        }

        auto res = nlohmann::ordered_json::array();
        for (auto i = 0; i < ranges.size(); ++i) {
            res.push_back(ranges[i]->to_json());
        }
        j["ranges"] = res;
        return j;
    }

    nlohmann::ordered_json PartitionRange::to_json() const {
        nlohmann::ordered_json j = DefiniteExpr::to_json_impl();
        j["partition_name"] = name.to_string();
        if (less_expr) {
            j["less_expr"] = less_expr->to_json();
        } else {
            j["less_expr"] = nlohmann::ordered_json{};
        }

        auto res = nlohmann::ordered_json::array();
        for (auto i = 0; i < options.size(); ++i) {
            res.emplace_back(options[i]->to_json());
        }
        j["options"] = res;
        if (range.first) {
            j["start"] = range.first->to_json();
        } else {
            j["start"] = nlohmann::ordered_json{};
        }
        if (range.second) {
            j["end"] = range.second->to_json();
        } else {
            j["end"] = nlohmann::ordered_json{};
        }
        return j;
    }

    nlohmann::ordered_json PartitionOption::to_json() const {
        nlohmann::ordered_json j = DefiniteExpr::to_json_impl();
        j["partition_type"] = type;
        j["str_value"] = str_value.to_string();
        return j;
    }
    nlohmann::ordered_json DatabaseOption::to_json() const {
        auto j = DefiniteExpr::to_json_impl();
        j["type"] = cantor::pb::ast::DatabaseOptionType_Name(type);
        j["str_value"] = str_value.to_string();
        return j;
    }
} // namespace cantor::parser
